package com.tanxi521.springboot.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.tanxi521.springboot.bean.NewRewardBean;
import com.tanxi521.springboot.bean.RewardBean;
import com.tanxi521.springboot.mapper.*;
import com.tanxi521.springboot.pojo.*;
import com.tanxi521.springboot.service.RewardsService;
import org.bukkit.configuration.file.YamlConfiguration;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class RewardsServiceImpl implements RewardsService {

    @Resource
    private RewardsMapper rewardsMapper;

    @Resource
    private MailBoxFileMapper mailBoxFileMapper;

    @Resource
    private RewardRecordMapper rewardRecordMapper;

    @Resource
    private MailBoxPlayerMapper mailBoxPlayerMapper;

    @Resource
    private VipPlayerMapper vipPlayerMapper;

    @Resource
    private RewardRecordCopyMapper rewardRecordCopyMapper;

    @Override
    public List<Rewards> queryListByRewards(Integer pageIndex, Integer pageSize, Rewards rewards) {
        Page page = new Page(pageIndex, pageSize);
        return rewardsMapper.selectListByRewards(page, rewards);
    }

    @Override
    public Rewards selectByPrimaryKey(Rewards rewards) {
        return rewardsMapper.selectByPrimaryKey(rewards.getId());
    }

    @Override
    public int queryRewardsCountByRewards(Rewards rewards) {
        return rewardsMapper.selectRewardsCountByRewards(rewards);
    }

    @Override
    public int updateRewardStateById(Rewards rewards) {
        return rewardsMapper.updateByPrimaryKey(rewards);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addNewRewards(NewRewardBean rewardBean, String username) {
        int updateCount = 0;
        String filename = UUID.randomUUID().toString().replace("-", "");
        Rewards rewards = rewardBean.getRewards();
        MailBoxFile mailBoxFile = rewardBean.getMailBoxFile();
        List<MailBoxItem> mailBoxItemList = rewardBean.getMailBoxItemList();

        rewards.setCreateBy(username);
        rewards.setRewardMailboxFile(filename);
        rewards.setCreateDate(new Date());
        rewards.setRewardIsOn((byte) 1);
        StringBuilder item = new StringBuilder("is:\n");
        int count = 0;
        for (int i = 1 ; i <= mailBoxItemList.size(); i++) {
            MailBoxItem mailBoxItem = mailBoxItemList.get(i - 1);
            if (mailBoxItem.getItem().equals("") || mailBoxItem.getItem() == null) continue;
            item.append("  is_" + i + ":\n" +
                    "    ==: org.bukkit.inventory.ItemStack\n" +
                    "    type: " + mailBoxItem.getItem() + "\n" +
                    "    damage: " + mailBoxItem.getDamage() + "\n" +
                    "    amount: " + mailBoxItem.getCount() + "\n");
            count ++;
        }
        item.append("  count: " + count + "\n");
        rewards.setRewardItem(item.toString());
        rewards.setRewardMoney(mailBoxFile.getCoin());
        rewards.setRewardPoint(mailBoxFile.getPoint());

//        YamlConfiguration des = new YamlConfiguration();
//        List<String> desList = new ArrayList<>();
//        desList.add(rewards.getRewardDescription());
//        des.set("descriptions", desList);
//        String descriptions = des.saveToString();
//
//        mailBoxFile.setType("player");
//        mailBoxFile.setFilename(filename);
//        mailBoxFile.setDescriptions(descriptions);
//        mailBoxFile.setItems(item.toString());

        updateCount += rewardsMapper.insert(rewards);
//        updateCount += mailBoxFileMapper.insert(mailBoxFile);

        return updateCount;
    }

    @Override
    public List<RewardCard> queryUserRewards(String userName) {
        List<RewardCard> rewardCards = rewardsMapper.selectRewardsByUserName(userName);
        VipPlayer vipPlayer = vipPlayerMapper.selectByPrimaryKey(userName);
        for (RewardCard rewardCard : rewardCards) {
            Integer restTime = rewardCard.getRestTime();
            Integer canReTotalTime = rewardCard.getCanReTotalTime();
            if (canReTotalTime - restTime <= 0) {
                rewardCard.setRestTime(0);
                rewardCard.setIsCanClick(false);
            } else {
                rewardCard.setRestTime(canReTotalTime - restTime);
                rewardCard.setIsCanClick(true);
            }
            if (!rewardCard.getRewardVipTag().equals("001")) {
                if (!ObjectUtil.isEmpty(vipPlayer)) {
                    String vip = vipPlayer.getVip();
                    String rewardVipTag = rewardCard.getRewardVipTag();
                    String substring = vip.substring(vip.length() - 1);
                    String substring1 = rewardVipTag.substring(rewardVipTag.length() - 1);
                    int playerVip = Integer.parseInt(substring);
                    int rewardVip = Integer.parseInt(substring1) - 1;
                    if (playerVip != rewardVip) rewardCard.setIsCanClick(false);
                } else {
                    rewardCard.setIsCanClick(false);
                }
            }
        }
        return rewardCards;
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public int giveUserRewards(RewardCard rewardCard, String userName) {
        VipPlayer vipPlayer = vipPlayerMapper.selectByPrimaryKey(userName);
        if (!rewardCard.getRewardVipTag().equals("001")) {
            if (!ObjectUtil.isEmpty(vipPlayer)) {
                String vip = vipPlayer.getVip();
                String rewardVipTag = rewardCard.getRewardVipTag();
                String substring = vip.substring(vip.length() - 1);
                String substring1 = rewardVipTag.substring(rewardVipTag.length() - 1);
                int playerVip = Integer.parseInt(substring);
                int rewardVip = Integer.parseInt(substring1) - 1;
                if (playerVip != rewardVip) {
                    return 0;
                }
            } else {
                return 0;
            }
        }
        int count = 0;
        Integer id = rewardCard.getId();
        Rewards rewards = rewardsMapper.selectByPrimaryKey(id);
        if (rewards.getRewardIsOn() == 0) {
            return rewards.getRewardIsOn();
        }
        if (rewardRecordMapper.selectByIdAndUserName(id, userName) >= rewards.getRewardLimit()) {
            return 0;
        }

        MailBoxFile mailBoxFile = new MailBoxFile();
        YamlConfiguration des = new YamlConfiguration();
        List<String> desList = new ArrayList<>();
        desList.add(rewards.getRewardDescription());
        des.set("descriptions", desList);
        String descriptions = des.saveToString();
        String filename = UUID.randomUUID().toString().replace("-", "");
        mailBoxFile.setType("player");
        mailBoxFile.setFilename(filename);
        mailBoxFile.setDescriptions(descriptions);
        mailBoxFile.setItems(rewards.getRewardItem());
        mailBoxFile.setCoin(rewards.getRewardMoney());
        mailBoxFile.setPoint(rewards.getRewardPoint());
        if (mailBoxFileMapper.insert(mailBoxFile) != 1) {
            try {
                throw new RuntimeException();
            } catch (RuntimeException e) {
                return 0;
            }
        }
        MailBoxPlayer mailBoxPlayer = new MailBoxPlayer();
        mailBoxPlayer.setSender("北境官网领取");
        mailBoxPlayer.setFilename(filename);
        mailBoxPlayer.setRecipient(userName);
        mailBoxPlayer.setSendtime(new Date());
        mailBoxPlayer.setText(rewards.getRewardDescription());
        mailBoxPlayer.setTopic(rewards.getRewardName());
        int insert = mailBoxPlayerMapper.insert(mailBoxPlayer);
        RewardRecord rewardRecord = new RewardRecord();
        rewardRecord.setRewardsId(rewards.getId());
        rewardRecord.setUserName(userName);
        rewardRecord.setRewardsDate(new Date());
        int insert1 = rewardRecordMapper.insert(rewardRecord);
        RewardRecordCopy rewardRecordCopy = new RewardRecordCopy();
        rewardRecordCopy.setRewardsDate(rewardRecord.getRewardsDate());
        rewardRecordCopy.setRewardsId(rewardRecord.getRewardsId());
        rewardRecordCopy.setUserName(userName);
        int insert2 = rewardRecordCopyMapper.insert(rewardRecordCopy);
        if (insert == 1 && insert1 == 1 && insert2 == 1) {
            count = insert + insert1 + insert2;
        } else {
            try {
                throw new RuntimeException();
            } catch (RuntimeException e) {
                return 0;
            }
        }
        return count;
    }

    @Override
    public int deleteById(List<Integer> ids) {
        return rewardsMapper.deleteByPrimaryKey(ids.get(0));
    }
}
